package com.autumn.mybatis.factory;

import javax.sql.DataSource;

import com.autumn.mybatis.provider.IDbProvider;

/**
 * 动态单路油数据源
 * 
 * @author 老码农
 *         <p>
 *         Description
 *         </p>
 * @date 2017-12-30 15:12:53
 */
public class DynamicSingleRoutingDataSource extends AbstractDynamicRoutingDataSource {

	/**
	 * 
	 * @param dataSourceFactory
	 * @param provider
	 */
	public DynamicSingleRoutingDataSource(IDataSourceFactory dataSourceFactory, IDbProvider provider) {
		super(dataSourceFactory, provider);
	}

	private DataSource singleDataSource = null;

	@Override
	protected DataSource getCurrentDataSource() {
		if (this.singleDataSource != null) {
			return this.singleDataSource;
		}
		synchronized (this) {
			if (this.singleDataSource != null) {
				return this.singleDataSource;
			}
			this.singleDataSource = this.getDataSourceFactory().getDataSource(null, this.getProvider());
			this.addDataSourceDestroy(singleDataSource);
			return this.singleDataSource;
		}
	}

}
